package com.github.prontera.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.github.prontera.SpringUtils;
import com.github.prontera.domain.SysDict;
import com.github.prontera.model.response.SysDictResponse;


/**
 * 数据字典类
 */
public final class CommonDict {

	private volatile static Map<String, List<SysDict>> dicts = new HashMap<String, List<SysDict>>();

	/**
	 * 初始化加载字典
	 */
	public static void init() {
		SysDictService sysDictService = SpringUtils.getBean(SysDictService.class);
		SysDict findSysDict=new SysDict();
		findSysDict.setStatus("0");
		findSysDict.setPageIndex(null);
		findSysDict.setPageSize(Integer.MAX_VALUE);
		findSysDict.setOrderBy("group_type,type,value");
		List<SysDict> sysDictSource = sysDictService.find2(findSysDict);
		String groupType = null;
		for (SysDict sysDict : sysDictSource) {
			if (!StringUtils.isEmpty(sysDict.getGroupType())) {
				if (StringUtils.isEmpty(groupType) || !groupType.equals(sysDict.getGroupType())) {
					List<SysDict> list = new ArrayList<SysDict>();
					list.add(sysDict);
					dicts.put(sysDict.getGroupType(), list);
				} else {
					dicts.get(groupType).add(sysDict);
				}
			}
			groupType = sysDict.getGroupType();
		}
	}

	/**
	 * 通过字典获取值
	 */
	public static Map<String, List<SysDict>> get() {
		return dicts;
	}

	/**
	 * 获取动态选择列表
	 */
	public static Map<String, List<SysDictResponse>> getFileds(String groupType) {
		if (StringUtils.isEmpty(groupType)) {
			throw new IllegalArgumentException("参数错误！");
		}
		List<SysDict> sysDicts = dicts.get(groupType);
		if (sysDicts == null) {
			return null;
		}
		String type = null;
		Map<String, List<SysDictResponse>> values = new HashMap<String, List<SysDictResponse>>();
		for (SysDict sysDict : sysDicts) {
			if (!StringUtils.isEmpty(sysDict.getType())) {
				SysDictResponse  sysDictSmall=new SysDictResponse();
				BeanUtils.copyProperties(sysDict,sysDictSmall);
				if(sysDict.getHasDefault()!=null&&sysDict.getHasDefault()==1){
					sysDictSmall.setIsDefault(true);
				}
				if(sysDict.getValue()!=null){
					sysDictSmall.setValue(sysDict.getValue());
				}
				sysDictSmall.setValueType(sysDict.getValueType());
				if (StringUtils.isEmpty(type) || !type.equals(sysDict.getType())) {
					List<SysDictResponse> list = new ArrayList<SysDictResponse>();
					list.add(sysDictSmall);
					values.put(sysDict.getType(), list);
				} else {
					values.get(type).add(sysDictSmall);
				}
			}
			type = sysDict.getType();
		}
		return values;
	}

	/**
	 * 通过字典获取值
	 */
	public static List<SysDict> get(String groupType) {
		if (StringUtils.isEmpty(groupType)) {
			throw new IllegalArgumentException("参数错误！");
		}
		return dicts.get(groupType);
	}

	/**
	 * 通过字典获取值
	 */
	public static SysDict get(String groupType, String type) {
		List<SysDict> sysDictSource = get(groupType);
		if (sysDictSource == null || sysDictSource.isEmpty()) {
			return null;
		}
		for (SysDict sysDict : sysDictSource) {
			if (sysDict.getType().equals(type)) {
				return sysDict;
			}
		}
		return null;
	}

}
